Ext.ns('Tm.file');
Tm.file.FileNavigatePanel = Ext
		.extend(
				Ext.Panel,
				{
					id : 'fileNavigatePanel',
					title : '文件导航',
					frame : true,
					autoWidth : true,
					// autoHeight : true,
					// layout : 'fit',
					
					initComponent : function() {

						var tree = new Ext.tree.TreePanel(
								{
									ref : 'tree',
									bodyBorder : true,
									frame : true,
									animate : true,
									enableDD : true,
									containerScroll : true,
									animate : true,
									// checkModel: 'cascade', // 对树的级联多选
									rootVisible:true,
									region : 'west',
									width : 200,
									split : true,
									title : '文件夹目录',
									autoScroll : true,
									margins : '5 0 5 5',
									listeners : {
										click : function(node) {
											if (node.id) {
												var filePanel=Ext.getCmp("fileNavigatePanel").filePanel;
												filePanel.setTitle(node.text);
												var store = filePanel.dataView.store;
												store.baseParams = {
													fileId : node.id
												};
												store.load();
											}
										}

									},

									loader : new Ext.tree.TreeLoader({
										dataUrl : 'file/loadAllFilelyFile.do'

									}),
									tbar : [
											' ',
											new Ext.form.TextField(
													{
														width : 120,
														emptyText : '请输入待查找的文件夹',
														enableKeyEvents : true,
														listeners : {
															keyup : function(
																	textField,
																	e) {

																var text = textField
																		.getValue();
																var navigatePanel = Ext
																		.getCmp("fileNavigatePanel");
																var tree = navigatePanel.tree;

																var re = new RegExp(
																		Ext
																				.escapeRe(text),
																		'i');
																if (!text) {
																	navigatePanel
																			.showAll(tree.root);
																	return;
																} else {
																	navigatePanel
																			.filterAll(
																					tree.root,
																					re);
																}

															}
														}
													}),
											' ',
											' ',
											{
												iconCls : 'icon-expand-all',
												tooltip : '全部展开',
												handler : function() {
													Ext
															.getCmp("fileNavigatePanel").tree.root
															.expand(true);
												},
												scope : this
											},
											'-',
											{
												iconCls : 'icon-collapse-all',
												tooltip : '全部收起',
												handler : function() {
													Ext
															.getCmp("fileNavigatePanel").tree.root
															.collapse(true);
												},
												scope : this
											} ]

								});
						var root = new Ext.tree.AsyncTreeNode({
							id:1,
							text : '目录',
							draggable : false
							//checked : false

						});
						tree.setRootNode(root);

						// tree.expandAll();
						// Set up images view

						var view = new Ext.DataView(
								{

									store : new Ext.data.JsonStore(
											{
												url : 'file/queryFileVoByFileId.do',
												id : 'id',
												fields : [
														'id',
														'name',
														{
															name : 'shortName',
															mapping : 'name',
															convert : Ext
																	.getCmp("fileNavigatePanel").shortName
														},
														'hasChildren',
														'type',
														'description',
														{
															name : 'shortDes',
															mapping : 'description',
															convert : Ext
																	.getCmp("fileNavigatePanel").shortDes
														} ],
												baseParams : {
														fileId : 1
													}
											}),
									tpl : new Ext.XTemplate(
											'<ul>',
											'<tpl for=".">',
											'<li class="phone">',
											'<img width="64" height="64" src="web/images/file/{[values.type.replace(/ /g, "-")]}.png"  />',
											'<strong>{shortName}</strong>',
											'<span>{shortDes}</span>', '</li>',
											'</tpl>', '</ul>'

									),
			
									plugins : [ new Ext.DataView.LabelEditor({
										dataIndex : 'id'
									}) ],
									id : 'phones',
									ref : 'dataView',
									itemSelector : 'li.phone',
									overClass : 'phone-hover',
									multiSelect : false,
									singleSelect :true,
									autoScroll : true,
									emptyText : '此文件夹为空',
									parentId:null
								});
						var filePanel = new Ext.Panel({
							ref : 'filePanel',
							bodyBorder : true,
							frame : true,
							title : '目录',
							region : 'center',
							margins : '5 5 5 0',
							layout : 'fit',
							items : view,
							
				
							tbar : [{
										iconCls : 'icon-arrow_up',
										tooltip : '上一级',
										handler : function() {
											var navigatePanel=Ext.getCmp("fileNavigatePanel");
											var dataView=navigatePanel.filePanel.dataView;
											var store = dataView.store;
											var fileId=store.baseParams.fileId;
											var root=navigatePanel.tree.root;
											navigatePanel.loadParent(root,fileId);

											
										},
										scope : this
									},
									'-',
									{
										iconCls : 'act_addIcon',
										text:'新增',
										tooltip : '新增文件夹或者文件',
										menu: [{
											iconCls : 'icon-folder',
											tooltip : '新增文件夹',
											text: '新增文件夹',
											handler : function() {
												
												var navigatePanel=Ext.getCmp("fileNavigatePanel");
												var dataView=navigatePanel.filePanel.dataView;
												var fileId=dataView.store.baseParams.fileId;
												
												var fileyFileAddPanel = new Tm.file.FileyFileAddPanel({parent:fileId
												});
												var fileyFileAddWindow = new Tm.common.CommonWindow({
													id : 'fileyFileAddWindow',
													title : '新增文件夹',
													items : fileyFileAddPanel
												});
												fileyFileAddWindow.on('close', function(panel) {
													var loader = new Ext.tree.TreeLoader({dataUrl:'file/loadAllFilelyFile.do',
														listeners : {
															load : function(  tree,   node,   callback ) {
																var fileNavigatePanel = Ext
																		.getCmp("fileNavigatePanel");
																fileNavigatePanel.tree.expandAll();
															}
														}
													
													});
													loader.load(tree.root);
													dataView.store.load();
												});
												fileyFileAddWindow.show();
											}
										},{
											iconCls : 'icon-file',
											tooltip : '新增文件',
											text: '新增文件',
											handler : function() {
												var navigatePanel=Ext.getCmp("fileNavigatePanel");
												var dataView=navigatePanel.filePanel.dataView;
												var store = dataView.store;
												var fileId=store.baseParams.fileId;
												
												var fileAddPanel = new Tm.file.FileAddPanel({parent:fileId
												});
												var fileAddWindow = new Tm.common.CommonWindow({
													id : 'fileAddWindow',
													title : '新增文件',
													items : fileAddPanel
												});
												fileAddWindow.on('close', function(panel) {
													
													 store.load();
												});
												fileAddWindow.show();
											}
										}]
									} ,
									'-',
									{
										iconCls : 'act_deleteIcon',
										tooltip : '删除',
										text: '删除',
										handler : function() {
											var navigatePanel=Ext.getCmp("fileNavigatePanel");
											var dataView=navigatePanel.filePanel.dataView;
											var records=dataView.getSelectedRecords();
											
											if(records.length>0){
												var record=records[0];
												console.dir(record);
												if(record.data.id){
													if( record.data.type=='FILE'&&record.data.hasChildren&&record.data.hasChildren=='true'){

														Ext.MessageBox.confirm('注意', '所选文件夹包含其他文件，您确定要对其进行删除操作？',
																function(btn) {
																	if (btn == 'yes') {
																		Ext.Ajax.request({
																			url : 'file/deleteFiles.do',
																			params : {
																				ids : [record.data.id]
																			},
																			success : function(response, action) {
																					var loader = new Ext.tree.TreeLoader({dataUrl:'file/loadAllFilelyFile.do',
																						listeners : {
																							load : function(  tree,   node,   callback ) {
																								var fileNavigatePanel = Ext
																										.getCmp("fileNavigatePanel");
																								fileNavigatePanel.tree.expandAll();
																							}
																						}
																					
																					});
																					loader.load(tree.root);
																					dataView.store.load();
																			} 

																		});
																	}else{
																		return;
																	}
																});
													}else{
														
														Ext.Ajax.request({
															url : 'file/deleteFiles.do',
															params : {
																ids : [record.data.id]
															},
															success : function(response, action) {
																	var loader = new Ext.tree.TreeLoader({dataUrl:'file/loadAllFilelyFile.do',
																		listeners : {
																			load : function(  tree,   node,   callback ) {
																				var fileNavigatePanel = Ext
																						.getCmp("fileNavigatePanel");
																				fileNavigatePanel.tree.expandAll();
																			}
																		}
																	
																	});
																	loader.load(tree.root);
																	dataView.store.load();
															} 

														});
													}

												}
												

											}
											
										},
										scope : this
									}]
							
						});

						Ext.apply(this, {
							layout : 'border',

							items : [ tree, filePanel ]
						});
						Tm.file.FileNavigatePanel.superclass.initComponent
								.call(this);

					},
					listeners : {
						render : function() {
							var fileNavigatePanel = Ext
									.getCmp("fileNavigatePanel");
							fileNavigatePanel.tree.expandAll();
							
							var dataView=fileNavigatePanel.filePanel.dataView;
							var store = dataView.store;
							store.load();
							
						},
						deactivate  : function( panel) {
							Ext.getCmp('fileSearchPanel').grid.store.load();
						}
					},

					filterAll : function(rootnode, regExp) {
						var childnodes = rootnode.childNodes;
						for ( var i = 0; i < childnodes.length; i++) {
							var node = childnodes[i];

							if (regExp.test(node.text)) {
								node.ui.show();
								node.parentNode.ui.show();
								// console.log(node.text);
							} else {
								node.ui.hide();
							}
							if (node.childNodes.length > 0) {
								this.filterAll(node, regExp);
							}
						}
					},
					loadParent : function(rootnode, fileId) {
						var childnodes = rootnode.childNodes;
						for ( var i = 0; i < childnodes.length; i++) {
							var node = childnodes[i];
							if(node.id==fileId){
								var filePanel=Ext.getCmp("fileNavigatePanel").filePanel;
								filePanel.setTitle(rootnode.text);
								var dataView=filePanel.dataView;
								
								var store = dataView.store;
								store.baseParams = {
										fileId : rootnode.id
									};
								store.load();
							}	
							if (node.childNodes.length > 0) {
								this.loadParent(node, fileId);
							}
						}
					},
					showAll : function(rootnode) {
						var childnodes = rootnode.childNodes;
						for ( var i = 0; i < childnodes.length; i++) {
							var node = childnodes[i];
							node.ui.show();
							if (node.childNodes.length > 0) {
								this.showAll(node); // 如果存在子节点 递归
							}
						}
					},
					shortName : function(name) {
						if (name.length > 15) {
							return name.substr(0, 12) + '...';
						}
						return name;
					},
					shortDes : function(des) {
						if (des.length > 15) {
							return des.substr(0, 12) + '...';
						}
						return des;
					}

				});
Ext.extend(Ext.DataView.LabelEditor, Ext.Editor, {
	alignment : "tl-tl",
	hideEl : false,
	cls : "x-small-editor",
	shim : false,
	completeOnEnter : true,
	cancelOnEsc : true,
	labelSelector : 'li.phone',

	init : function(view) {
		this.view = view;
		view.on('render', this.initEditor, this);
	},

	initEditor : function() {
		this.view.getEl().on('dblclick', this.onMouseDown, this, {
			delegate : this.labelSelector
		});
	},

	onMouseDown : function(e, target) {
		var item = this.view.findItemFromChild(target);
		// e.stopEvent();
		var record = this.view.store.getAt(this.view.indexOf(item));
		var data = record.data;
		if (data.type == 'FILE') {
			//enter the file
//			var store = this.view.store;
//			store.baseParams = {
//				fileId : data.id
//			};
//			store.load();

			
 
			
			var fileyFileAddPanel = new Tm.file.FileyFileAddPanel({dataId:data.id
			});
			var fileyFileAddWindow = new Tm.common.CommonWindow({
				id : 'fileyFileAddWindow',
				title : '更新文件夹',
				items : fileyFileAddPanel
			});
			fileyFileAddWindow.on('close', function(panel) {
				var fileNavigatePanel = Ext.getCmp("fileNavigatePanel");
				var loader = new Ext.tree.TreeLoader({dataUrl:'file/loadAllFilelyFile.do',
					listeners : {
						load : function(  tree,   node,   callback ) {
							
							fileNavigatePanel.tree.expandAll();
						}
					}
				
				});
				loader.load(fileNavigatePanel.tree.root);
				fileNavigatePanel.filePanel.dataView.store.load();
			});
			fileyFileAddWindow.show();
		
			
			
			
			
			
		} else {
			var fileAddPanel = new Tm.file.FileAddPanel({
				dataId : data.id
			});
			var fileAddWindow = new Tm.common.CommonWindow({
				id : 'fileAddWindow',
				title : '更新文件',
				items : fileAddPanel
			});
			fileAddWindow.on('close', function(panel) {
				
				Ext.getCmp('fileNavigatePanel').filePanel.dataView.store.load();
			});
			fileAddWindow.show();
		}

	}
});

